package net.qiujuer.sample.genius.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

import net.qiujuer.genius.ui.widget.AbsSeekBar;
import net.qiujuer.genius.ui.widget.Button;
import net.qiujuer.genius.ui.widget.Text;
import net.qiujuer.genius.ui.widget.TextField;
import net.qiujuer.genius.ui.widget.view.OnSeekBarChangeListener;
import net.qiujuer.genius.ui.widget.view.SeekParams;
import net.qiujuer.sample.genius.ResourceTable;
import net.qiujuer.sample.genius.util.ToastUtil;

public class SeekBarAbilitySlice extends AbilitySlice {
    private AbsSeekBar seekBar;
    private TextField min_text_field, max_text_field;
    private Button mBtChange;
    private float minValue = 0;
    private float maxValue = 10;
    private Text text;
    private int mCurrentProgress = 0;

    private Text min_text_field_text;
    private Text max_text_field_text;

    private AnimatorProperty leftTextAnimator;
    private AnimatorProperty rightTextAnimator;
    private final int ANIMATOR_DURATION = 10;
    private final int TRANSPARENT = 0;
    private final int NON_TRANSPARENT = 1;
    private final int MOVE_Y_DIS = 100;
    private final float SCALE_MATRIX = 1.33f;
    private final float DEFAULT_SCALE_MATRIX = 1f;
    private boolean growOutFromNothingLeft = false;
    private boolean growOutFromNothingRight = false;
    private TextFieldFocusedListener textFieldFocusedListener;
    private boolean isTouch;
    static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        //进行判断，如果是在1秒以内，则返回true，意思是不让其点击事件响应
        if (0 < timeD && timeD < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_seek_bar);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_colorTitle));
        text = (Text) findComponentById(ResourceTable.Id_text);
        seekBar = (AbsSeekBar) findComponentById(ResourceTable.Id_java_build);
        seekBar.setMax(10);
        findComponentById(ResourceTable.Id_iv_left).setClickedListener(comp -> terminate());
        min_text_field = (TextField) findComponentById(ResourceTable.Id_min_text_field);
        max_text_field = (TextField) findComponentById(ResourceTable.Id_max_text_field);
        min_text_field_text = (Text) findComponentById(ResourceTable.Id_min_text_field_text);
        max_text_field_text = (Text) findComponentById(ResourceTable.Id_max_text_field_text);
        min_text_field.setText("0");
        max_text_field.setText("10");
        min_text_field_text.setVisibility(Component.VISIBLE);
        max_text_field_text.setVisibility(Component.VISIBLE);
        leftTextAnimator = min_text_field_text.createAnimatorProperty();
        rightTextAnimator = max_text_field_text.createAnimatorProperty();
        initTextFieldListener();
        mBtChange = (Button) findComponentById(ResourceTable.Id_bt_change);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekParams seekParams) {
                mCurrentProgress = seekParams.progress;
                text.setText("Min:" + (int) seekBar.getMin() + ", " + "Max:" + (int) seekBar.getMax() + ", " + "Value:" + mCurrentProgress);
                System.out.println("mCurrentProgress:" + mCurrentProgress);
            }

            @Override
            public void onStartTrackingTouch(AbsSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(AbsSeekBar seekBar) {

            }
        });

        mBtChange.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                if (isTouch) {
                    if (isFastDoubleClick()) {
                        return;
                    }
                    if (!min_text_field.getText().toString().trim().equals("") || !max_text_field.getText().trim().equals("")) {
                        if (!min_text_field.getText().toString().trim().equals("")) {
                            minValue = Float.parseFloat(min_text_field.getText().toString().trim());
                        } else {
                            minValue = Float.parseFloat(max_text_field.getText().toString().trim());
                        }
                        if (!max_text_field.getText().toString().trim().equals("")) {
                            maxValue = Float.parseFloat(max_text_field.getText().toString().trim());
                        }
                        try {
                            getUITaskDispatcher().asyncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.println("minValue = " + minValue + "maxValue = " + maxValue);
                                    if (seekBar != null) {
                                        if (minValue > maxValue) {
                                            return;
                                        }
                                        if (minValue > 0||minValue < 0) {
                                            seekBar.setMin(minValue);
                                            seekBar.setProgress(minValue);
                                            seekBar.setMax(maxValue);
                                            seekBar.setTickCount((int) (maxValue - minValue) + 1);
                                        }
                                        if (maxValue > 0||maxValue < 0) {
                                            seekBar.setMin(minValue);
                                            seekBar.setProgress(minValue);
                                            seekBar.setMax(maxValue);
                                            seekBar.setTickCount((int) (maxValue - minValue) + 1);
                                        }
                                        if (minValue == maxValue) {
                                            seekBar.setMin(minValue - 1);
                                            seekBar.setProgress(minValue + 1);
                                            seekBar.setMax(minValue);
                                            seekBar.setTickCount(2);
                                            System.out.println("minValue2 = " + minValue + "maxValue2 = " + maxValue);
                                        }
                                        if(minValue == 0 && maxValue==0){
                                            seekBar.setMin(minValue - 1);
                                            seekBar.setProgress(minValue + 1);
                                            seekBar.setMax(minValue);
                                            seekBar.setTickCount(2);
                                        }
                                        text.setText("Min:" + (int) seekBar.getMin() + ", " + "Max:" + (int) maxValue + ", " + "Value:" + seekBar.getProgress());
//                                        if (min_text_field.getText().trim() != null) {
//                                            text.setText("Min:" + (int) seekBar.getMin() + ", " + "Max:" + (int) maxValue + ", " + "Value:" + seekBar.getProgress());
//                                        } else if (max_text_field.getText().trim() != null) {
//                                            text.setText("Min:" + (int) minValue + ", " + "Max:" + (int) seekBar.getMax() + ", " + "Value:" + seekBar.getProgress());
//                                        } else {
//                                            text.setText("Min:" + (int) seekBar.getMin() + ", " + "Max:" + (int) seekBar.getMax() + ", " + "Value:" + seekBar.getProgress());
//                                        }
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        mBtChange.setClickable(false);
                    }
                } else {
                    mBtChange.setClickable(false);
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    private void initTextFieldListener() {

        min_text_field.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                int[] locationOnScreen = min_text_field.getLocationOnScreen();
//                int marginScreenY = locationOnScreen[1];
                if (textFieldFocusedListener != null) {
                    System.out.println("ScrollY");
                    textFieldFocusedListener.leftTextFieldFocused(min_text_field.isFocused());
                }
            }
        });

        max_text_field.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                int[] locationOnScreen = max_text_field.getLocationOnScreen();
//                int marginScreenY = locationOnScreen[1];
                if (textFieldFocusedListener != null) {
                    textFieldFocusedListener.rightTextFieldFocused(max_text_field.isFocused());
                }
            }
        });

        min_text_field.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean isFocused) {
                min_text_field_text.setVisibility(Component.VISIBLE);
                if (isFocused) {
                    min_text_field.setFocusedColor(min_text_field_text);
                } else {
                    min_text_field.setEmptyColor(min_text_field_text);
                }
            }
        });

        max_text_field.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean isFocused) {
                max_text_field_text.setVisibility(Component.VISIBLE);
                if (isFocused) {
                    max_text_field.setFocusedColor(max_text_field_text);
                } else {
                    max_text_field.setEmptyColor(max_text_field_text);
                }
            }
        });

        min_text_field.addTextObserver(new ohos.agp.components.Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                min_text_field_text.setVisibility(Component.VISIBLE);
                isTouch = true;
                getAbility().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String min_text = min_text_field.getText().trim();
                            String max_text = max_text_field.getText().trim();
                            if ((Integer.parseInt(min_text) > (Integer.parseInt(max_text))) || (Integer.parseInt(min_text)) > 100001) {
                                ToastUtil.showToast(getContext(), "非法输入");
                                mBtChange.setClickable(false);
                                if (!growOutFromNothingLeft) {
                                    growOutFromNothingLeft = true;
                                    leftTextAnimator.moveFromY(MOVE_Y_DIS);
                                    leftTextAnimator.moveToY(min_text_field_text.getTop());
                                    leftTextAnimator.scaleXFrom(SCALE_MATRIX);
                                    leftTextAnimator.scaleYFrom(SCALE_MATRIX);
                                    leftTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                                    leftTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                                    leftTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                                    min_text_field_text.setVisibility(Component.VISIBLE);
                                }
                                System.out.println("=====非法输入========");
                                return;
                            } else {
                                if (Integer.parseInt(min_text) == minValue) {
                                    mBtChange.setClickable(false);
                                    min_text_field_text.setVisibility(Component.VISIBLE);
                                } else {
                                    mBtChange.setClickable(true);
                                    min_text_field_text.setVisibility(Component.VISIBLE);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            mBtChange.setClickable(false);
                            min_text_field_text.setVisibility(Component.VISIBLE);
                        }
                        if (s == null || s.length() == 0) {
                            growOutFromNothingLeft = false;

                            leftTextAnimator.moveFromY(min_text_field_text.getTop());
                            leftTextAnimator.moveToY(MOVE_Y_DIS);
                            leftTextAnimator.scaleXFrom(DEFAULT_SCALE_MATRIX);
                            leftTextAnimator.scaleYFrom(DEFAULT_SCALE_MATRIX);
                            leftTextAnimator.scaleX(SCALE_MATRIX);
                            leftTextAnimator.scaleY(SCALE_MATRIX);
                            leftTextAnimator.setDuration(ANIMATOR_DURATION).alpha(TRANSPARENT).start();
                            min_text_field_text.setVisibility(Component.INVISIBLE);
                        } else {
                            if (!growOutFromNothingLeft) {
                                growOutFromNothingLeft = true;

                                leftTextAnimator.moveFromY(MOVE_Y_DIS);
                                leftTextAnimator.moveToY(min_text_field_text.getTop());
                                leftTextAnimator.scaleXFrom(SCALE_MATRIX);
                                leftTextAnimator.scaleYFrom(SCALE_MATRIX);
                                leftTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                                leftTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                                leftTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                                min_text_field_text.setVisibility(Component.VISIBLE);
                            }
                        }
                    }
                });
            }
        });

        max_text_field.addTextObserver(new ohos.agp.components.Text.TextObserver() {

            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                max_text_field_text.setVisibility(Component.VISIBLE);
                isTouch = true;
                try {
                    String min_text = min_text_field.getText().trim();
                    String max_text = max_text_field.getText().trim();
                    if ((Integer.parseInt(min_text) > (Integer.parseInt(max_text)) || (Integer.parseInt(max_text)) > 100001)) {
                        mBtChange.setClickable(false);
                        if (!growOutFromNothingRight) {
                            growOutFromNothingRight = true;
                            rightTextAnimator.moveFromY(max_text_field_text.getTop() + MOVE_Y_DIS);
                            rightTextAnimator.moveToY(max_text_field_text.getTop());
                            rightTextAnimator.scaleXFrom(SCALE_MATRIX);
                            rightTextAnimator.scaleYFrom(SCALE_MATRIX);
                            rightTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                            rightTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                            rightTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                            max_text_field_text.setVisibility(Component.VISIBLE);
                        }
                        return;
                    } else {
                        if (Integer.parseInt(max_text) == maxValue) {
                            mBtChange.setClickable(false);
                            max_text_field_text.setVisibility(Component.VISIBLE);
                        } else {
                            mBtChange.setClickable(true);
                            max_text_field_text.setVisibility(Component.VISIBLE);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    max_text_field_text.setVisibility(Component.VISIBLE);
                    mBtChange.setClickable(false);
                }
                if (s == null || s.length() == 0) {
                    growOutFromNothingRight = false;
                    rightTextAnimator.moveFromY(max_text_field_text.getTop());
                    rightTextAnimator.moveToY(max_text_field_text.getTop() + MOVE_Y_DIS);
                    rightTextAnimator.scaleXFrom(DEFAULT_SCALE_MATRIX);
                    rightTextAnimator.scaleYFrom(DEFAULT_SCALE_MATRIX);
                    rightTextAnimator.scaleX(SCALE_MATRIX);
                    rightTextAnimator.scaleY(SCALE_MATRIX);
                    rightTextAnimator.setDuration(ANIMATOR_DURATION).alpha(TRANSPARENT).start();
                    max_text_field_text.setVisibility(Component.INVISIBLE);
                } else {
                    if (!growOutFromNothingRight) {
                        growOutFromNothingRight = true;
                        rightTextAnimator.moveFromY(max_text_field_text.getTop() + MOVE_Y_DIS);
                        rightTextAnimator.moveToY(max_text_field_text.getTop());
                        rightTextAnimator.scaleXFrom(SCALE_MATRIX);
                        rightTextAnimator.scaleYFrom(SCALE_MATRIX);
                        rightTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                        rightTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                        rightTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                        max_text_field_text.setVisibility(Component.VISIBLE);
                    }

                }
            }
        });
    }

    public interface TextFieldFocusedListener {
        void leftTextFieldFocused(boolean isFocused);

        void rightTextFieldFocused(boolean isFocused);
    }
}
